<template>
  <div class="app-container">
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="6" :offset="0">
        <el-card shadow="always" >
          <div slot="header">
            <div style="display:flex;justify-content:space-between">
              <span>可用余额（元）</span>
              <el-button style="margin-left:auto" @click="handleCreate" type="primary" size="mini" >申请提现</el-button>
            </div>
          </div>
          <!-- card body -->
          <span style="font-weight: bold;">1200.00</span>
        </el-card>
        
      </el-col>
      <el-col :span="6" :offset="0">
        <el-card shadow="always" >
          <div slot="header">
            <div style="display:flex;justify-content:space-between">
              <span>累计收入（元）</span>
            </div>
          </div>
          <!-- card body -->
          <span style="font-weight: bold;">1200.00</span>
        </el-card>
      </el-col>
      <el-col :span="6" :offset="0">
        <el-card shadow="always" >
          <div slot="header">
            <div style="display:flex;justify-content:space-between">
              <span>待结算金额（元）</span>
            </div>
          </div>
          <!-- card body -->
          <span style="font-weight: bold;">{{detail.no_settle}}.00</span>
        </el-card>
      </el-col>
      <el-col :span="6" :offset="0">
        <el-card shadow="always" >
          <div slot="header">
            <div style="display:flex;justify-content:space-between">
              <span>冻结金额（元）</span>
            </div>
          </div>
          <!-- card body -->
          <span style="font-weight: bold;">1200.00</span>
        </el-card>
      </el-col>
    </el-row>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="交易时间" prop="created_time"  align="center" />
      <el-table-column label="提款账号" prop="account"  align="center" />
      <el-table-column label="开户人" prop="name"  align="center" />
      <el-table-column label="提现金额"  align="center">
        <template slot-scope="{row}">
          <span>￥{{row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col">
        <template slot-scope="{row}">
          <el-tag :type="row.status===1?'success':'danger'">
            {{ row.status | statusFilter }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog title="提现" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="提现金额" prop="price">
          <el-input-number v-model="temp.price" :min="0" label="请输入金额" />
        </el-form-item>
        <el-form-item label="提现账户" prop="payment_id">
          <el-select v-model="temp.payment_id" placeholder="请选择">
            <el-option
              v-for="item in payments"
              :key="item.id"
              :label="item.account"
              :value="item.id">
              <span style="float: left">{{ item.account }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          返回
        </el-button>
        <el-button type="primary" @click="createData">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import {
  fetchAssets,
  Cashout,
  fetchPayments
} from '@/api/pay'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const statusOptions = {
  0: '审核中',
  1: '已提现',
  2:'提现失败'
}

export default {
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      return statusOptions[status]
    }

  },
  data() {
    return {
      detail:{
        no_settle:30
      },
      payments: [],
      dialogVisible: false,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
      },
      statusOptions,
      temp: {
        price: 0,
        payment_id: undefined
      },
      dialogFormVisible: false,
      rules: {
        price: [{
          trigger: 'blur',
          validator:(rule,value,callback)=>{
              if(!value){
               return callback(new Error('提现金额必须大于0'))
              }
              if(value>this.detail.no_settle){
               return callback(new Error(`提现金额不能大于${this.detail.no_settle}`))
              }
              callback()
          }
        }],
        payment_id: [{
          required: true,
          message: '提款账号不能为空',
          trigger: 'change'
        }]
      }
    }
  },
  created() {
    this.getList()
    fetchPayments().then(res=>{
      this.payments=res.data.items
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchAssets(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
  
    resetTemp() {
      this.temp = {
        price: 0,
        payment_id: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          Cashout(this.temp).then((res) => {
            this.list.unshift(res.data)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}

</script>
